import { getHttp, putHttp } from "../../../api";
import { useRequest, useSetState } from 'ahooks'
import React, { useEffect } from 'react';
import { NavBar, Toast, SwipeCell, Button, Empty, Notify, Cell } from 'react-vant'
import style from "./style.module.scss";
import { useNavigate } from "react-router-dom";
import url1 from '../../../assets/images/image/hdpi/doctor_avatar.png'
import { LazyImageNotFount } from "../../../components";


const Index: React.FC = () => {

    const navigate = useNavigate();
    // 跳转聊天
    const goChat = (v: any) => {
        navigate(`/consultation/chat?userId=${v.userId}&nickName=${v.nickName}&recordId=${v.recordId}`)
    }

    // 处理评论时间
    const timestampToTime = (timestamp: any) => {
        timestamp = timestamp ? timestamp : null;
        const date = new Date(timestamp);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
        const Y = date.getFullYear() + '-';
        const M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
        const D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + ' ';
        const h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
        const m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
        const s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
        return Y + M + D + h + m + s;
    }

    // 问诊记录列表接口
    const getConsultation = () => getHttp('/health/doctor/inquiry/verify/v1/findInquiryRecordList');
    const { data: ConsultationList = [] } = useRequest(async () => {
        const resp = await getConsultation();
        return resp.result;
    });
    console.log(ConsultationList);

    // 结束问诊
    const Endconsultation = (statu: any, recordId: any) => {
        if (statu == 1) {
            window.location.reload()
            Notify.show({ type: 'success', message: '当前问诊已结束' })
            putHttp(`/health/doctor/inquiry/verify/v1/endInquiry?recordId=${recordId}`);
        }else if(statu==2){
            Notify.show({ type: 'success', message: '请评价该医生' })
        }else if(statu==3){
            Notify.show({ type: 'danger', message: '问诊已结束' })
        }
    }

    return (
        <div>
            {/* 导航栏 */}
            <NavBar
                title="问诊列表"
                leftText='返回'
                onClickLeft={() => {
                    navigate(-1)
                }}
            />
            {/* 问诊列表 */}
            {
                ConsultationList.length > 0 ? ConsultationList.map((v: any, i: any) => (
                    <div key={i}>
                        <SwipeCell
                            rightAction={
                                <Button style={{ height: '100%', width: '100px' }} onClick={() => Endconsultation(v.status, v.recordId)} square type="danger">
                                    结束问诊
                                </Button>
                            }
                        >
                            <div className={style['consultationlist']} onClick={() => goChat(v)}>
                                <div className={style['left']}>
                                    <LazyImageNotFount src={v.userHeadPic} alt="" className={style['img']} />
                                </div>
                                <div className={style['right']}>
                                    <span className={style['name']}>{v.nickName} <span>{timestampToTime(v.inquiryTime)}</span> </span>
                                    <span className={style['lastchat']}>{v.lastContent}</span>
                                </div>
                            </div>
                        </SwipeCell>
                    </div>
                )) : <Empty
                    className="custom-image"
                    imageSize={90}
                    image={<LazyImageNotFount src={url1} alt=""/>}
                    description="暂无问诊记录"
                />
            }
        </div>
    )
}


export default Index;
